package com.labot.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlRootElement;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.hibernate.annotations.AccessType;
import org.hibernate.search.annotations.Field;

import com.labot.authentication.encryp.EncryptDecryptUtils;
import com.labot.exceptions.ExceptionDomain;
import com.labot.types.ConstantsGrupsType;
import com.labot.types.ConstantsType;

/**
 * This class represents the basic "Config" object in AppFuse that allows for authentication
 * and user management.  It implements Acegi Security's UserDetails interface.
 *
 * @author Ing. Enrique Barbeito
 * 
 */
@Entity
@Table(name = "app_config")
@AccessType("field")
@XmlRootElement
public class Config extends BaseObject  implements Serializable {
    private static final long serialVersionUID = 3832626162173359411L;

   
	@Field
	@Column(nullable = false, length = 50, unique = true)
    @Enumerated(EnumType.STRING)
    private ConstantsType constant;
    
	@Field
	@Column(length = 20)
    @Enumerated(EnumType.STRING)
    private ConstantsGrupsType configGroup;
    
    @Field
    @Column(nullable = false, length = 255)
    private String value;
    
    @Transient
    public static final String OBSCURED_PASSWORD = "***************";
    
    /**
     * Default constructor - creates a new instance with no values set.
     */
    public Config() {
    }

    
    public ConstantsType getConstant() {
        return constant;
    }


	public String getValue() {
		if(constant != null) {
			if (constant.toString().toLowerCase().contains("password")) {
				return EncryptDecryptUtils.decrypt(value);
			}
		}
        return value;
    }
    
    public String getValueEncoded() {
    	if(constant != null) {
			if (constant.toString().toLowerCase().contains("password")) {
				return OBSCURED_PASSWORD;
			}
		}
    	return value;
    }
    


    
    public void setConstant(ConstantsType constant) {
		this.constant = constant;
	}


	public void setValue(String value) throws ExceptionDomain {
		if(value == null) {
			throw new ExceptionDomain();
		}
		this.value = value;
		if(constant != null) {
			if (constant.toString().toLowerCase().contains("password")) {
				this.value = EncryptDecryptUtils.encrypt(value);
			}
		}
	}


	public ConstantsGrupsType getConfigGroup() {
		return configGroup;
	}


	public void setConfigGroup(ConstantsGrupsType configGroup) {
		this.configGroup = configGroup;
	}


	/**
     * {@inheritDoc}
     */
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Config)) {
            return false;
        }

        final Config config = (Config) o;

        return (config.getConstant().equals(this.getConstant()));

    }

    /**
     * {@inheritDoc}
     */
    public int hashCode() {
        return (id != null ? id.hashCode() : 0);
    }

    /**
     * {@inheritDoc}
     */
    public String toString() {
        ToStringBuilder sb = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE)
        		.append("id", this.getId())
                .append("constant", this.getConstant())
                .append("value", this.getValue());
        return sb.toString();
    }
}
